﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;
//using System.Guid;

namespace E01
{
    /// <summary>
    /// 同步客户
    /// </summary>
    public class A04 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {
            E01A04P P = (E01A04P)handler.Serial.Deserialize(st, typeof(E01A04P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[3];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 255);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 255);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 255);
                sps[2].Value = P.Name;

                String sql = "Begin tran declare @flag int declare @FID int declare @result int set @flag = 0 set @result = 0 " +
                             " select @FID = isnull(FItemID,-10) from t_Organization where F_102 = @ID " +
                             " if(@FID is not null and @FID > 0) Begin " +
                             "    update t_Organization set FShortNumber = @Number,FNumber = @Number,FName = @Name,F_102=@ID where FItemID = @FID set @flag = @flag + @@ERROR " +
                             "    update t_Item set FName = @Name,FNumber=@Number,FShortNumber=@Number,FFullNumber=@Number  where FItemID = @FID set @flag = @flag + @@ERROR  set @result = 1 " +
                             " END Else Begin EXEC GetICMaxNum 't_Item',@FID OUTPUT " +
                             "  INSERT INTO t_Item (FItemID,FItemClassID,FParentID,FLevel,FName,FNumber,FShortNumber,FFullNumber,FDetail,UUID,FDeleted) " +
                             "    VALUES (@FID,1,0,1,@Name,@Number,@Number,@Number,1,cast(NEWID() as varchar(36)),0) set @flag = @flag + @@ERROR " +
                             "  INSERT INTO t_Organization (FHelpCode,FShortName,FAddress,FStatus,FRegionID,FTrade,FContact,FPhone,FMobilePhone,FFax,FPostalCode,FEmail," +
                             "    FBank,FAccount,FTaxNum,FIsCreditMgr,FSaleMode,FValueAddRate,FCity,FProvince,FCountry,FHomePage,Fcorperate,FCarryingAOS,FTypeID, " +
                             "    FSaleID,FStockIDKeep,FCoSupplierID,FCyID,FSetID,FCIQNumber,FARAccountID,FPreAcctID,FOtherARAcctID,FPayTaxAcctID,FAPAccountID," +
                             "    FPreAPAcctID,FOtherAPAcctID,FfavorPolicy,Fdepartment,Femployee,FlastTradeAmount,FlastRPAmount,FmaxDealAmount,FminForeReceiveRate,FminReserverate," +
                             "    FdebtLevel,FPayCondition,FNameEN,FAddrEn,FCIQCode,FRegion,FManageType,FShortNumber,FNumber,FName,FParentID,FItemID,F_102) " +
                             "    VALUES (NULL,NULL,NULL,1072,0,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,1057,17,NULL,NULL,NULL,NULL,NULL,0,0,0,0,0,0,0,NULL," +
                             "    0,0,0,0,0,0,0,NULL,0,0,0,0,0,1,1,0,0,NULL,NULL,NULL,0,0,@Number,@Number,@Name,0,@FID,@ID) " +
                             " set @flag = @flag + @@ERROR 	set @result = 2 End " +
                             " If @flag = 0 " +
                             " Begin " +
                             " commit tran select 1,@FID,@result " +
                             " End " +
                             " Else Begin " +
                             "       rollBack " +
                             "       select 2,@FID,@result " +
                             " End";

                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql, sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "1")
                    {
                        if (rs.Data.Tables[0].Rows[0][2].ToString() == "1")
                        {
                            result.Result = EnumResult.Update;
                            result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                            result.Message = String.Format("客户同步完成:{0},{1}", P.Number, result.Result.ToString());
                        }
                        else
                        {
                            result.Result = EnumResult.Insert;
                            result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                            result.Message = String.Format("客户同步完成:{0},{1}", P.Number, result.Result.ToString());
                        }
                    }
                    else
                    {
                        result.Result = EnumResult.None;
                        result.Message = rs.Message;
                        result.Message = String.Format("客户同步失败:{0}", rs.Message);
                    }
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = rs.Message;
                    result.Message = String.Format("客户同步失败:{0}", rs.Message);
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = ex.Message;
            }
        }

    }

    /// <summary>
    /// 客户
    /// </summary>
    public class E01A04P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        public String Name;
        /// <summary>
        /// 客户简码
        /// </summary>
        public String ShortNumber;
        /// <summary>
        /// 增值税率
        /// </summary>
        public Decimal ValueAddRate;
        /// <summary>
        /// 销售模式
        /// </summary>
        //public String FSaleMode;
        /// <summary>
        /// 全名
        /// </summary>
        public String FullName;
        /// <summary>
        /// 客户类别 1客户 4供应商
        /// </summary>
        public int ItemClassID;
        /// <summary>
        /// 所处层级
        /// </summary>
        //public int Level;
        /// <summary>
        /// 是否最下层级
        /// </summary>
        //public Boolean Detail;
        /// <summary>
        /// 使用标记
        /// </summary>
        public int UseSign;
        
    }
}
